--
-- Author: shunguo.chen
-- Date: 2019-05-05 16:05:48
--
-- JewelTargetRender 宝石转换的目标render
--

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self._iconRender = IconRender.create(self._ui.icon)
    self._iconRender:setClickEnable(false)

    display.uiAddClick(self._ui.Panel, function()
        executeFunc(self._onClick, self)
    end)

    self:refresh()
end

function ClassRef:refresh()
    if self._data then
        self._iconRender:setData(self._data)
        self._ui.name:setString(self._data:getName(true))
        self._ui.data:setString(self._data:getDesc())

        local iconList = self._data:getPosShowIconList()
        local iconNode, icon
        for i=1,6 do
            iconNode = self._ui["icon" .. i]
            if iconNode then
                icon = iconList[i]
                iconNode:setVisible(icon ~= nil)
                if icon then
                    iconNode:loadTexture(icon)
                end
            end
        end
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
